package cn.iocoder.yudao.module.of.dal.mysql.schedulecenter;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.of.dal.dataobject.schedulecenter.ScheduleCenterDO;
import cn.iocoder.yudao.module.of.dal.dataobject.schedulepart.SchedulePartDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.of.controller.admin.schedulecenter.vo.*;

/**
 * 日程中心 信息 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface ScheduleCenterMapper extends BaseMapperX<ScheduleCenterDO> {

    default PageResult<ScheduleCenterDO> selectPage(ScheduleCenterPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ScheduleCenterDO>()
                .eqIfPresent(ScheduleCenterDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ScheduleCenterDO::getParticipants, reqVO.getParticipants())
                .betweenIfPresent(ScheduleCenterDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ScheduleCenterDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(ScheduleCenterDO::getLocation, reqVO.getLocation())
                .eqIfPresent(ScheduleCenterDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ScheduleCenterDO::getIcon, reqVO.getIcon())
                .eqIfPresent(ScheduleCenterDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ScheduleCenterDO::getReminder, reqVO.getReminder())
                .eqIfPresent(ScheduleCenterDO::getType, reqVO.getType())
                .eqIfPresent(ScheduleCenterDO::getRemindTimeType, reqVO.getRemindTimeType())
                .betweenIfPresent(ScheduleCenterDO::getRemindTime, reqVO.getRemindTime())
                .eqIfPresent(ScheduleCenterDO::getRepeatPeriod, reqVO.getRepeatPeriod())
                .eqIfPresent(ScheduleCenterDO::getAttachments, reqVO.getAttachments())
                //.eqIfPresent(ScheduleCenterDO::getTags, reqVO.getTags())
                .eqIfPresent(ScheduleCenterDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ScheduleCenterDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ScheduleCenterDO::getId));
    }

    /**
     *
     * 获取参与者
     * @param reqVO
     * @return
     */
    default List<ScheduleCenterDO> selectList(ScheduleCenterPageReqVO reqVO) {
        MPJLambdaWrapperX<ScheduleCenterDO> wrapper = new MPJLambdaWrapperX<>();
        wrapper.distinct();
        wrapper.likeIfPresent(ScheduleCenterDO::getTitle, reqVO.getTitle())
                .inIfPresent(ScheduleCenterDO::getParticipants, reqVO.getParticipants())
                .betweenIfPresent(ScheduleCenterDO::getStartTime, reqVO.getCreateTime())
                .eqIfPresent(ScheduleCenterDO::getLocation, reqVO.getLocation())
                .eqIfPresent(ScheduleCenterDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ScheduleCenterDO::getIcon, reqVO.getIcon())
                .eqIfPresent(ScheduleCenterDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ScheduleCenterDO::getReminder, reqVO.getReminder())
                .eqIfPresent(ScheduleCenterDO::getType, reqVO.getType())
                .eqIfPresent(ScheduleCenterDO::getRemindTimeType, reqVO.getRemindTimeType())
                .betweenIfPresent(ScheduleCenterDO::getRemindTime, reqVO.getRemindTime())
                .eqIfPresent(ScheduleCenterDO::getRepeatPeriod, reqVO.getRepeatPeriod())
                .eqIfPresent(ScheduleCenterDO::getAttachments, reqVO.getAttachments())
              //  .eqIfPresent(ScheduleCenterDO::getTags, reqVO.getTags())
                .eqIfPresent(ScheduleCenterDO::getStatus, reqVO.getStatus());

        wrapper.and(wrap->wrap.eq(SchedulePartDO::getUserId,WebFrameworkUtils.getLoginUserId())
                .or().eq(ScheduleCenterDO::getCreator,WebFrameworkUtils.getLoginUserId())
            );
           wrapper.leftJoin(SchedulePartDO.class,SchedulePartDO::getScId,ScheduleCenterDO::getId);

           wrapper.orderByDesc(ScheduleCenterDO::getId);
           return  selectList( wrapper);
    }

    /**
     * 获取日程创建者
     * @param reqVO
     * @return
     */
    default List<ScheduleCenterDO> selectListByCreator(ScheduleCenterPageReqVO reqVO) {
        MPJLambdaWrapperX<ScheduleCenterDO> wrapper = new MPJLambdaWrapperX<>();

        wrapper.eqIfPresent(ScheduleCenterDO::getTitle, reqVO.getTitle())
                .inIfPresent(ScheduleCenterDO::getParticipants, reqVO.getParticipants())
                .betweenIfPresent(ScheduleCenterDO::getStartTime, reqVO.getCreateTime())
                .eqIfPresent(ScheduleCenterDO::getLocation, reqVO.getLocation())
                .eqIfPresent(ScheduleCenterDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ScheduleCenterDO::getIcon, reqVO.getIcon())
                .eqIfPresent(ScheduleCenterDO::getDescription, reqVO.getDescription())
                .eqIfPresent(ScheduleCenterDO::getReminder, reqVO.getReminder())
                .eqIfPresent(ScheduleCenterDO::getType, reqVO.getType())
                .eqIfPresent(ScheduleCenterDO::getRemindTimeType, reqVO.getRemindTimeType())
                .betweenIfPresent(ScheduleCenterDO::getRemindTime, reqVO.getRemindTime())
                .eqIfPresent(ScheduleCenterDO::getRepeatPeriod, reqVO.getRepeatPeriod())
                .eqIfPresent(ScheduleCenterDO::getAttachments, reqVO.getAttachments())
               // .eqIfPresent(ScheduleCenterDO::getTags, reqVO.getTags())
                .eqIfPresent(ScheduleCenterDO::getStatus, reqVO.getStatus())
                .eq(ScheduleCenterDO::getCreator,WebFrameworkUtils.getLoginUserId());

        wrapper.orderByDesc(ScheduleCenterDO::getId);
        return  selectList( wrapper);
    }

    List findByDetailId();
}